package com.kitchen.service.impl;

import com.kitchen.beans.entity.Collect;
import com.kitchen.common.vo.ResStatus;
import com.kitchen.common.vo.ResultVO;
import com.kitchen.mapper.CollectMapper;
import com.kitchen.service.CollectService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

@Service
public class CollectServiceImpl implements CollectService {
    @Autowired
    private CollectMapper collectMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO add(Integer uid, Integer fid) {
        Example example=new Example(Collect.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uId",uid);
        criteria.andEqualTo("fId",fid);
        List<Collect> collects = collectMapper.selectByExample(example);
        if (collects.size()==0){
            Collect collect=new Collect();
            collect.setuId(uid);
            collect.setfId(fid);
            collect.setCoTime(new Date());
            int i = collectMapper.insert(collect);
            if (i>0){
                return new ResultVO(ResStatus.OK,"success",null);
            }
            else {
                return new ResultVO(ResStatus.NO,"error",null);
            }
        }
        return new ResultVO(ResStatus.NOSC,"error",null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultVO delete(Integer[] coid) {
        int deleteall = collectMapper.deleteall(coid);
        if (deleteall>0){
            return new ResultVO(ResStatus.OK,"success",null);
        }
        return new ResultVO(ResStatus.NO,"error",null);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ResultVO list(Integer uid) {
        Example example=new Example(Collect.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("uId",uid);
        List<Collect> collects = collectMapper.selectByExample(example);
        if (collects.size()>0){
            return new ResultVO(ResStatus.OK,"success",collects);
        }
        return new ResultVO(ResStatus.NO,"error",null);
    }
}
